<?php
/**
 * Created by PhpStorm.
 * User: 吕金鹏
 * Date: 2021/6/10
 * Time: 1:15 下午
 */

namespace app\index\controller;
use think\Db;
use think\facade\Request;
use think\Model;
use app\index\model\doCate;
class Wei extends Base
{
    public function index(){
        $pwork = input("pwork");
        $zhizheng = input('zhizheng');
        $xuexi = input('xuexi');
        $status = input("status");
        $if_teacher = input('if_teacher');
        $where = [];
        if($pwork){
            $where[] = ['pwork','=',$pwork]; 
        }
        if($xuexi){
            $where[] = ['xuexi','like','%'.$xuexi.'%']; 
        }
        if($zhizheng){
            // $zhizheng = json_encode($zhizheng);
            $where[] =  ['zhizheng','like',"%".$zhizheng."%"];
        }
        if($status){
            $where[] =  ['status','=',$status];
            if($status==5){
                $where[] =  ['status','>=',5];
            }
        }
        // dump($where);die;
        if($if_teacher){
            $where[] =  ['if_teacher','=',$if_teacher];
        }
        $list = Db::name("renzheng")->where("area",$this->admin['area'])->where($where)->orderRaw("status=2 desc")->paginate(10,false,['query' => Request::instance()->param()]);
//        dump($list);die;
        $this->assign("list",$list);
        $this->assign("page",$list->render());
        return $this->fetch();
    }
    public function memberinfo(){
        $id = input('id');
        $member = Db::name("renzheng")->where("id",$id)->find();
        $work = json_decode($member['work'],true);

        $this->assign('work',$work);

        $peixun = json_decode($member['peixun'],true);

        $this->assign('peixun',$peixun);

        $this->assign('admin',$member);
        
        $xueduan = json_decode($member['xueduan'],true);
        $this->assign('xueduan',$xueduan);
        
        $zhizheng = explode(',',$member['zhizheng']);
        $this->assign('zhizheng',$zhizheng);
        // dump($zhizheng);die;
        
        $zsfield = json_decode($member['zsfield'],true);
        $this->assign("zsfield",$zsfield);
        
        $cate = Db::name("cate")->where('status',1)->where('id','in','2,52,68')->select();

        $area = Db::name("cate")->where("id",$member['area'])->find();
        $school = Db::name("cate")->where("id",$member['school'])->find();
        
        $this->assign('area',$area);
        $this->assign('school',$school);
        $this->assign('cate',$cate);
        // $this->assign('member',$member);
        return $this->fetch();
    }
    public function changestatus(){
        $id = input('id');
        if(is_array($id)){
//            dump($id);
            $id_str ='';
            foreach($id as $key=>$val){
                if($key==0){
                    $id_str = $val;
                }else{
                    $id_str .=','.$val;
                }
            }
            $res = Db::name('renzheng')->where("id",'in',$id_str)->update(['status'=>5]);
        }else{
            $res = Db::name("renzheng")->where('id',$id)->update(['status'=>5]);
        }
        ajaxMsg('1','提交成功');
    }
    public function nochangestatus(){
        $id = input('id');
        $msg = input('msg');
        $res = Db::name("renzheng")->where('id',$id)->update(['status'=>4,'jiaowei_message'=>$msg]);
        ajaxMsg('1','提交成功');
    }
    public function renzheng(){
        $school_list = Db::name("admin")->where("area",$this->admin['area'])->select();
        $id_str ='';
        foreach($school_list as $key=>$val){

            if($key==0){
                $id_str.= $val['id'];
            }else{
                $id_str.= ','.$val['id'];
            }
        }
        $school = Db::name("school_renzheng")->where("admin_id",'in',$id_str)->paginate(10);
        $this->assign("list",$school);
        $this->assign("page",$school->render());
        return $this->fetch();
    }
    public function schoolinfo(){
        $id = input("id");
        $info = Db::name("school_renzheng")->where('id',$id)->find();
        $gongneng = json_decode($info['gongneng'],true);
        $this->assign('gongneng',$gongneng);
        $class = json_decode($info['class_info'],true);
        $this->assign('class',$class);
        $teacher = json_decode($info['teacher_info'],true);
        $this->assign('teacher',$teacher);
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function ketishenqing(){
        $id = input("id");
        $nian = input("nian");
        if(!$nian){
            $list = Db::name("keti")->select();
            $ary =[];
            foreach($list as $val){
                $ary[] = $val['year'];
            }
            $ary = array_unique($ary);

            $this->assign("nian",$ary);
            return $this->fetch("chosenian");
        }
        if($id==''){
            $list = Db::name("keti")->where("year",$nian)->select();
            $this->assign("list",$list);
            $this->assign('nian',$nian);
            return $this->fetch("step1");
        }
        $this->assign("nian",$nian);
        $this->assign("id",$id);
        $order = input('order');
        $order_str = '';
        if(!$order){
            $order_str = 'sq.a_fen desc';
        }else{
            $order_str = 'sq.a_fen '.$order;
        }
        $if_inportant = input("if_inportant");
        $school = input("school");
        $where = [];
        $where['sq.cid'] = $id;
        $str = '';
        if($if_inportant){
            $where['if_inportant'] = $if_inportant;
        }
        if($school){
            $school = Db::name("cate")->whereLike('title','%'.$school.'%')->select();
           
            if($school){
                foreach($school as $key=>$val){
                    if($key==0){
                        $str=$val['id'];
                    }else{
                        $str.=','.$val['id'];
                    }
                }
            }
        }
        //1 学校待审核 2 学校拒绝 3 中心待审核 4 教委待审核 5 教委拒绝 6 审核通过 7 中心审核拒绝 
        if($str){
            $list = Db::name("shenqing as sq")
                    ->leftjoin('keti kt','kt.id=sq.cid')
                    ->leftjoin("admin ad",'ad.id=sq.admin_id')
                    ->where("ad.area",$this->admin['area'])
                    ->where("sq.status",'not in',"0,1,2,5")
                    ->where("school",'in',$str)
                    ->order($order_str)
                    ->field("kt.name,sq.count1,sq.count2,sq.message,sq.status,sq.id,sq.time,sq.if_re,sq.if_two,sq.content,ad.name as relname,sq.school_id,sq.a_fen as fen,sq.if_inportant")
                    ->paginate(10,false,['query'=>request()->param()]);
        }else{
            $list = Db::name("shenqing as sq")
                    ->leftjoin('keti kt','kt.id=sq.cid')
                    ->leftjoin("admin ad",'ad.id=sq.admin_id')
                    ->where("ad.area",$this->admin['area'])
                    ->where("sq.status",'not in',"0,1,2,5")
                    ->order($order_str)
                    ->field("kt.name,sq.count1,sq.count2,sq.message,sq.status,sq.id,sq.time,sq.if_re,sq.if_two,sq.content,ad.name as relname,sq.school_id,sq.if_inportant,sq.a_fen as fen,sq.if_inportant")
                    ->paginate(10,false,['query'=>request()->param()]);
        }
        if($this->admin['rid']==13){
            $list = Db::name("shenqing as sq")
                ->leftjoin('keti kt','kt.id=sq.cid')
                ->leftjoin("admin ad",'ad.id=sq.admin_id')
                ->leftjoin('cate c','c.id=ad.school')
                // ->where("sq.school_id",$this->admin['school'])
                ->where("pid",'not in',[2,52])
                ->where($where)
                // ->where("ad.area","in",[2,52])
                
                ->where('sq.status','<>','0')
                ->where('sq.status','<>','-1')
                ->order($order_str)
                ->field("kt.name,sq.count1,sq.count2,sq.message,sq.status,sq.id,sq.time,sq.if_re,sq.if_two,sq.content,sq.if_inportant,ad.name as aname,sq.s_fen as fen,sq.if_inportant")
                ->paginate(10,false,['query'=>request()->param()]);
        }
        $this->assign('page',$list->render());
        //根据学校种类.生成审核部门
        $title = [];
        //查询学校分类
        $title[0] = '教委审核';
        $title[1] = '中心审核';
        
        $list = $list->toArray();
        $new_ary = [];
        // dump($list['data']);die;
        foreach($list['data'] as $key=>$val){
            $new_ary[$key] = $val;
            $new_ary[$key]['content'] = json_decode($val['content'],true);
            $new_ary[$key]['ctitle'] = Db::name("cate")->where('id',$val['school_id'])->find()['title'];
            // $new_ary[$key]['fen'] = Db::name("dafen as df")
            //                         ->join('admin a','df.admin_id=a.id')
            //                         ->where('a.area',$this->admin['area'])
            //                         ->where("sid",$val['id'])
            //                         ->avg('fen');
            foreach($title as $k=>$v){
                $new_ary[$key]['statusdes'][$k] = getstatus($v,$val['status']);
            }
            //查看打分的数量
            $count1 = Db::name("dafen as df")
                    ->join('admin a','df.admin_id=a.id')
                    ->where('a.area',$this->admin['area'])
                    ->where("sid",$val['id'])
                    ->count();
            //查看可以打分专家数量
            $count2 = Db::name("admin")->where("area",$this->admin['area'])->where("rid",12)->where("z_type",'in',['全部课题',$val['if_inportant']])->count();
            if($count1==$count2){
                $new_ary[$key]['shenhe'] = 1;
            }else{
                $new_ary[$key]['shenhe'] = 0;
            }
        }
        $title[0] = '区教育局审核';
        $title[1] = '市中心审核';
        $this->assign('title',$title);
        $this->assign('data',$new_ary);
        return $this->fetch();
    }
    public function chakan(){
        $id = input('id');
        $type = input('type');
        $info = Db::name('shenqing')->where('id',$id)->find();
        $this->assign("info",$info);
        $content = json_decode($info['content'],true);
        $content['s_name'] = isset($content['s_name'])?array_values($content['s_name']):'';
        $content['s_num'] =isset($content['s_num'])?array_values($content['s_num']):'';
        $content['s_price'] = isset($content['s_price'])?array_values($content['s_price']):'';
        $content['s_year'] = isset($content['s_year'])?array_values($content['s_year']):'';
        $content['member_name'] = isset($content['member_name'])?array_values($content['member_name']):'';
        $content['member_zhiye'] = isset($content['member_zhiye'])?array_values($content['member_zhiye']):'';
        $content['member_year'] = isset($content['member_year'])?array_values($content['member_year']):'';
        $content['member_zhuanye'] = isset($content['member_zhuanye'])?array_values($content['member_zhuanye']):'';
        $content['member_danwei'] = isset($content['member_danwei'])?array_values($content['member_danwei']):'';
        $content['member_fengong'] = isset($content['member_fengong'])?array_values($content['member_fengong']):'';
        $content['cate_title'] = isset($content['cate_title'])?array_values($content['cate_title']):'';
        $content['class_title'] = isset($content['class_title'])?array_values($content['class_title']):'';
        $content['p_time'] = isset($content['p_time'])?array_values($content['p_time']):'';
        $content['if_finish'] = isset($content['if_finish'])?array_values($content['if_finish']):'';
        // dump($content);die;
        $this->assign('content',$content);
        $this->assign("if_two",$info['if_two']);
        if($type==1){
            //查看打分详情
            // $dafen_list = Db::name('dafen as df')
            //             ->leftjoin("admin a",'a.id=df.admin_id')
            //             ->where("df.sid",$info['id'])
            //             ->where("area",$this->admin['area'])
            //             ->field("df.*,a.name")
            //             ->select();
            
            $dafen_list = Db::name('admin')
                        // ->Leftjoin('dafen df','a.id=df.admin_id')
                        // ->where("df.sid",$info['id'])
                        ->where("area",$this->admin['area'])
                        ->where('rid',12)
                        ->where('z_type','in',[$info['if_inportant'],'全部课题'])
                        // ->field("df.*,a.name")
                        ->select();
            foreach($dafen_list as $key=>$val){
                $if_exit = Db::name('dafen')->where('sid',$info['id'])->where('admin_id',$val['id'])->find();
                if($if_exit){
                    $dafen_list[$key]['fen'] = $if_exit['fen'];
                }else{
                    $dafen_list[$key]['fen'] = 0;
                }
                
            }
            // dump($dafen_list);die;
            $this->assign("dafen",$dafen_list);
            return $this->fetch('school/kanfen');
        }
        return $this->fetch();
    }
    public function agreen(){
        $id = input("id");
        
        if(Request::isPost()){
            $data = input("post.");
            $sq = Db::name("shenqing")->where("id",$id)->find();
            //查看所属教委
            $cate = Db::name('cate')->where("id",$sq['school_id'])->find();
            $f_cate = Db::name("cate")->where("pid",$cate['id'])->select();
            $str = '';
            foreach($f_cate as $key=>$val){
                if($key==0){
                    $str = $val['id'];
                }else{
                    $str .= ','.$val['id'];
                }
            }
            //查看学校通过的命额
             //1 学校待审核 2 学校拒绝 3 中心待审核 4 教委待审核 5 教委拒绝 6 审核通过 7 中心审核拒绝 
            $count = Db::name('shenqing')->where('cid',$sq['cid'])->where("status",'in','3,6')->where('school_id','in',$str)->count();
            //查看可以通过名额
            $cate = "其他中小学/中职/特教学校";
          
            $member_class = Db::name("member_class")->where("cid",$sq['cid'])->where("type",$cate)->find();
            if($member_class['num'] <=$count){
               ajaxMsg(0,'通过人员已到上限');
            }
            if($sq['message']){
               $info['message'] = $sq['message']."<br/>教委意见:".$data['message']; 
            }else{
                $info['message'] = "教委意见:".$data['message'];
            }
            // $info['count1'] = $data['count'];
            $info['status'] = 3;
            $res = Db::name("shenqing")->where("id",$data['id'])->update($info);
            if($res){
                ajaxMsg(1,'审核成功');
            }
            ajaxMsg(0,'网络错误,审核失败');
        }
        $this->assign("id",$id);
        return $this->fetch();
    }
    public function disagreen(){
        $id = input("id");
        
        if(Request::isPost()){
            $data = input("post.");
            $sq = Db::name("shenqing")->where("id",$id)->find();
            // dump($data);die;
            if($sq['message']){
               $info['message'] = $sq['message']."<br/>教育局意见:".$data['message']; 
            }else{
                $info['message'] = "教育局意见:".$data['message'];
            }
            
            $info['if_re'] = $data['if_re'];
            $info['status'] = 2;
            $res = Db::name("shenqing")->where("id",$data['id'])->update($info);
            if($res){
                ajaxMsg(1,'拒绝成功');
            }
            ajaxMsg(0,'网络错误,审核失败');
        }
        $this->assign("id",$id);
        return $this->fetch();
    }
    public function addschool(){
        $if_exit = Db::name("cate")->where("title",$data['title'])->find();
        if($if_exit){
            ajaxMsg(0,'学校名称已存在！');
        }
        $data['title'] = input("title");
        $data['pid'] = $this->admin['area'];
        $data['status'] =1;
        $data['if_own'] = 0;
        $data['lv'] = 3;
        $res = Db::name("cate")->insert($data);
        if($res){
            ajaxMsg(1,'新增成功');
        }
        ajaxMsg(0,'网络开小差了,稍后再试');
    }
    public function school(){
        // echo $this->admin['area'];die;
        $cate = Db::name("cate")->where("pid",$this->admin['area'])->paginate(10);
        // dump($cate);die;
        $this->assign('cate',$cate);
        $this->assign("page",$cate->render());
        
        return $this->fetch();
    }
    public function editschool(){
         $id = input('id');
        if (Request::isPost()){
            $post = input('post.');
            if (doCate::update($post)){
                return json(['code'=>1]);
            }else{
                return json(['code'=>0]);
            }
        }else{
            $data = doCate::get($id);
            $this->assign('data',$data);
            return $this->fetch();
        }
    }
    public function delschool(){
        $data = input('post.');
        if ($data['status'] == 1){
            $data['status'] = 2;
        }else{
            $data['status'] = 1;
        }
        $res = doCate::update($data);
        if ($res){
            return json(['code'=>1]);
        }else{
            return json(['code'=>0]);
        }
    }
    public function xuexi(){
          $list = Db::name("renzheng")->where("area",$this->admin['area'])->where("status",10)->order("id desc")->paginate(10,false,['query' => Request::instance()->param()])->each(function($item){
            $item['nian'] = 0;
            $pwork = json_decode($item['pwork'],true);
            // [{"t_stime":"2009-06-10","t_etime":"2021-06-10"}]
            foreach($pwork as $key=> $val){
                $item['nian'] += strtotime($val["t_etime"]) - strtotime($val["t_stime"]);
            }
            $item['school_name'] = Db::name('cate')->where("id",$item['school'])->find()['title'];
            $item['zhizheng'] = implode(',',json_decode($item['zhizheng'],true));
            $item['nian'] = Sec2Time($item['nian']);
            return $item;
        });
          $this->assign('list',$list);
          $this->assign('page',$list->render());
          return $this->fetch();
    }
    public function weichangestatus(){
        $id = input('id');
        $id_str ='';
        $statuts = input('status');
            foreach($id as $key=>$val){
                if($key==0){
                    $id_str = $val;
                }else{
                    $id_str .=','.$val;
                }
            }
            $res = Db::name("renzheng")->where("id",'in',$id_str)->update(['status'=>$statuts]);
            if($res){
               return json(['code'=>1,'msg'=>'审核成功']); 
            }
        return json(['code'=>0,'msg'=>'网络延时，请稍后再试']); 
    }
}